package com.yvon.maple.service.system.service.impl;

import com.yvon.boot.mybatis.BaseTreeServiceImpl;
import com.yvon.maple.service.system.mapper.PermissionMapper;
import com.yvon.maple.service.system.entity.Permission;
import com.yvon.maple.service.system.service.PermissionService;
import com.yvon.maple.service.system.vo.MetaVo;
import com.yvon.maple.service.system.vo.RouterVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author : Yvon
 * @since : 2021-09-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PermissionServiceImpl extends BaseTreeServiceImpl<Permission, PermissionMapper> implements PermissionService {


    @Override
    public List<RouterVo> routerList() {
        List<Permission> list = list();
        List<Permission> childs = getChilds(list, 0L);
        return buildMenus(childs);
    }

    @Override
    public List<RouterVo> buildMenus(List<Permission> menus) {
        List<RouterVo> routers = new LinkedList<>();
        for (Permission menu : menus) {
            RouterVo router = new RouterVo();
            router.setHidden(menu.getVisible() == 0);
            router.setName(getRouteName(menu));
            router.setPath(getRouterPath(menu));
            router.setComponent(getComponent(menu));
            router.setIsFrame(menu.getIframe() == 1 ? "0" : "1");
            router.setMeta(new MetaVo(menu.getName(), menu.getIcon(), menu.getCache() == 0 ,menu.getRemark()));
            List<Permission> cMenus = (List<Permission>) menu.getChildren();
            if (!cMenus.isEmpty() && cMenus.size() > 0 && 0 == menu.getType()) {
                router.setAlwaysShow(true);
                router.setRedirect("noRedirect");
                router.setChildren(buildMenus(cMenus));
            } else if (isMenuFrame(menu)) {
                List<RouterVo> childrenList = new ArrayList<>();
                RouterVo children = new RouterVo();
                children.setPath(menu.getPath());
                children.setComponent(menu.getComponent());
                children.setName(StringUtils.capitalize(menu.getPath()));
                children.setIsFrame(menu.getIframe() == 1 ? "0" : "1");
                children.setMeta(new MetaVo(menu.getName(), menu.getIcon(), menu.getCache() == 0, menu.getRemark()));
                childrenList.add(children);
                router.setChildren(childrenList);
            }
            routers.add(router);
        }
        return routers;
    }

    @Override
    public List<Permission> listDataByLevel(int level, Long parentId) {
        List<Permission> listData = new ArrayList<>();
        level--;
        List<Permission> childrenList = findChildListByParentId(parentId);
        for (Permission dto : childrenList) {
            if (0 == dto.getTreeLeaf() && level != 0) {
                dto.setChildren(this.listDataByLevel(level, dto.getId()));
            } else {
                dto.setChildren(new ArrayList<>());
            }
            listData.add(dto);
        }
        return listData;
    }

    @Override
    public Integer getMaxSort(Long parentId) {
        Integer maxSort = findMaxSort(parentId);
        return maxSort / 10 * 10 + 10;
    }



    private List<Permission> getByIds(Collection<Long> ids) {
        return lambdaQuery()
                .in(Permission::getId, ids)
                .list();
    }

    /**
     * 获取组件信息
     *
     * @param menu 菜单信息
     * @return 组件信息
     */
    public String getComponent(Permission menu) {
        String component = "Layout";
        if (StringUtils.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu)) {
            component = menu.getComponent();
        }
        else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu)) {
            component = "ParentView";
        }
        return component;
    }


    /**
     * 获取路由名称
     *
     * @param menu 菜单信息
     * @return 路由名称
     */
    public String getRouteName(Permission menu) {
        String routerName = StringUtils.capitalize(menu.getPath());
        // 非外链并且是一级目录（类型为目录）
        if (isMenuFrame(menu)) {
            routerName = StringUtils.EMPTY;
        }
        return routerName;
    }

    /**
     * 是否为菜单内部跳转
     *
     * @param menu 菜单信息
     * @return 结果
     */
    public boolean isMenuFrame(Permission menu) {
        return 0 == menu.getParentId() && 1 == menu.getType()
                && menu.getIframe() == 0;
    }

    /**
     * 是否为parent_view组件
     *
     * @param menu 菜单信息
     * @return 结果
     */
    public boolean isParentView(Permission menu) {
        return 0 != menu.getParentId() && 0 == menu.getType();
    }

    /**
     * 获取路由地址
     *
     * @param menu 菜单信息
     * @return 路由地址
     */
    public String getRouterPath(Permission menu) {
        String routerPath = menu.getPath();
        // 非外链并且是一级目录（类型为目录）
        if (0==menu.getParentId() && 0==menu.getType()
                && menu.getIframe() == 0) {
            routerPath = "/" + menu.getPath();
        }
        // 非外链并且是一级目录（类型为菜单）
        else if (isMenuFrame(menu))
        {
            routerPath = "/";
        }
        return routerPath;
    }
}
